home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15 / Blue / FSSpecification.cp < prev    next >
Encoding:
Text File  |  1996-02-19  |  17.1 KB  |  485 lines  |  [TEXT/CWIE]

  1.  
  2. #include "FSSpecification.h"
  3. #include "MoreStrings.h"
  4. #include "MoreAEM.h"
  5. #include "AbstractData.h"
  6.  
  7. //
  8. // For 'EqualString'
  9. //
  10. #include <TextUtils.h>
  11.  
  12. //========================================================================================
  13. // CLASS TFSSpecification
  14. //========================================================================================
  15.  
  16.  
  17. //----------------------------------------------------------------------------------------
  18. // TFSSpecification::TFSSpecification: 
  19. //----------------------------------------------------------------------------------------
  20. TFSSpecification::TFSSpecification()
  21. {
  22.     fFileSpec.vRefNum = 0;
  23.     fFileSpec.parID = 0;
  24. } // TFSSpecification::TFSSpecification 
  25.  
  26. //----------------------------------------------------------------------------------------
  27. // TFSSpecification::TFSSpecification: 
  28. //----------------------------------------------------------------------------------------
  29. TFSSpecification::TFSSpecification(const FSSpec& fromSpecification)
  30. {
  31.     fFileSpec = fromSpecification;
  32. } // TFSSpecification::TFSSpecification 
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // TFSSpecification::TFSSpecification: 
  36. //----------------------------------------------------------------------------------------
  37. TFSSpecification::TFSSpecification(const TFSSpecification& fromSpecification)
  38. {
  39.     fFileSpec = fromSpecification;
  40. } // TFSSpecification::TFSSpecification 
  41.  
  42. //----------------------------------------------------------------------------------------
  43. // TFSSpecification::TFSSpecification: 
  44. //----------------------------------------------------------------------------------------
  45. TFSSpecification::TFSSpecification(const TDescriptor& fromDesc, Boolean forceCreate /*= false*/, OSType creator /*= 0*/, OSType fileType /*= 0*/)
  46. {
  47.     //
  48.     // We should have coersion handlers to go from typeChar (a pathname) to typeFSS
  49.     //
  50.     if(fromDesc.DescriptorType() == typeChar)
  51.     {
  52.         TString fileSpecName(fFileSpec.name);
  53.         
  54.         fFileSpec.vRefNum = 0;
  55.         fFileSpec.parID = 0;
  56.         fileSpecName = fromDesc;
  57.     }
  58.     else
  59.         fromDesc.GetDescriptorData(TUpdataDataReference(typeFSS, (Ptr)&fFileSpec, sizeof(FSSpec), sizeof(FSSpec)));
  60.     
  61.     //
  62.     // Make sure that the fileSpec is valid
  63.     //
  64.     if(forceCreate)
  65.     {
  66.         FSpCreate(&fFileSpec, creator, fileType, 0);
  67.     }
  68.     FSMakeFSSpec(fFileSpec.vRefNum, fFileSpec.parID, fFileSpec.name, &fFileSpec);
  69. } // TFSSpecification::TFSSpecification 
  70.  
  71. //----------------------------------------------------------------------------------------
  72. // TFSSpecification::TFSSpecification: 
  73. //
  74. // This finds the applicatio file that the given psn was launched from
  75. //----------------------------------------------------------------------------------------
  76. TFSSpecification::TFSSpecification(const ProcessSerialNumber& psn)
  77. {
  78.     ProcessInfoRec info;
  79.     info.processInfoLength = sizeof(ProcessInfoRec);
  80.     info.processName = nil;
  81.     info.processAppSpec = &fFileSpec;
  82.     info.processLocation = nil;
  83.     GetProcessInformation(&psn, &info);
  84. } // TFSSpecification::TFSSpecification 
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // TFSSpecification::~TFSSpecification
  88. //----------------------------------------------------------------------------------------
  89. TFSSpecification::~TFSSpecification()
  90. {
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // TFSSpecification::operator=: 
  95. //----------------------------------------------------------------------------------------
  96. const TFSSpecification& TFSSpecification::operator=(const TFSSpecification& rhs)
  97. {
  98.     fFileSpec = rhs;
  99.  
  100.     return *this;
  101. } // TFSSpecification::operator= 
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // TFSSpecification::operator==: 
  105. //----------------------------------------------------------------------------------------
  106. Boolean TFSSpecification::operator==(const TFSSpecification& rhs) const
  107. {
  108.     FSSpec compareFSSpec = rhs;
  109.     
  110.     return (    (fFileSpec.vRefNum == compareFSSpec.vRefNum) &&
  111.                 (fFileSpec.parID == compareFSSpec.parID) &&
  112.                 (EqualString(fFileSpec.name, compareFSSpec.name, false, true)));
  113. } // TFSSpecification::operator== 
  114.  
  115. //----------------------------------------------------------------------------------------
  116. // TFSSpecification::Open: 
  117. //----------------------------------------------------------------------------------------
  118. OSStatus TFSSpecification::Open(OpenFileRefNum& refNum, SInt8 permission) const
  119. {
  120.     if(permission == kDefaultPerm)
  121.         permission = fsRdWrPerm;
  122.     return FSpOpenDF(&fFileSpec, permission, &refNum);
  123. } // TFSSpecification::Open 
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // TFSSpecification::OpenResFile: 
  127. //----------------------------------------------------------------------------------------
  128. OSStatus TFSSpecification::OpenResFile(OpenResFileRefNum& refNum, SInt32 permission) const
  129. {
  130.     if(permission == kDefaultPerm)
  131.         permission = fsRdPerm;
  132.     refNum = FSpOpenResFile(&fFileSpec, permission);
  133.     return ResError();
  134. } // TFSSpecification::Open 
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // TFSSpecification::Delete: 
  138. //----------------------------------------------------------------------------------------
  139. OSStatus TFSSpecification::Delete()
  140. {
  141.     return FSpDelete(&fFileSpec);
  142. } // TFSSpecification::Delete 
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // TFSSpecification::Move: 
  146. //----------------------------------------------------------------------------------------
  147. OSStatus TFSSpecification::Move(TFSSpecification& destinationOfMove)
  148. {
  149.     FSSpec fileSpecOfDestinationSpec = destinationOfMove;
  150.     
  151.     return FSpCatMove(&fFileSpec, &fileSpecOfDestinationSpec);
  152. } // TFSSpecification::Move 
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // TFSSpecification::Name: 
  156. //----------------------------------------------------------------------------------------
  157. OSStatus TFSSpecification::Name(TString& fileName) const
  158. {
  159.     const TString fileSpecName((unsigned char*)fFileSpec.name);
  160.     fileName = fileSpecName;
  161.     
  162.     // CopyMemory(&fFileSpec.name[0], &fileName[0], fFileSpec.name[0] + 1);
  163.     
  164.     return noErr;
  165. } // TFSSpecification::Name 
  166.  
  167. //----------------------------------------------------------------------------------------
  168. // TFSSpecification::operator FSSpec
  169. //----------------------------------------------------------------------------------------
  170. TFSSpecification::operator FSSpec() const
  171. {
  172.     return fFileSpec;
  173. } // TFSSpecification::operator FSSpec
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // TFSSpecification::Rename: 
  177. //----------------------------------------------------------------------------------------
  178. OSStatus TFSSpecification::Rename(const TString& newName)
  179. {
  180.     return FSpRename(&fFileSpec, newName);
  181. } // TFSSpecification::Rename 
  182.  
  183. //----------------------------------------------------------------------------------------
  184. // TFSSpecification::ExchangeFiles: 
  185. //----------------------------------------------------------------------------------------
  186. OSStatus TFSSpecification::ExchangeFiles(TFSSpecification& exchangeWith)
  187. {
  188.     FSSpec fileSpecOfExchangeFile = exchangeWith;
  189.     
  190.     return FSpExchangeFiles(&fFileSpec, &fileSpecOfExchangeFile);
  191. } // TFSSpecification::ExchangeFiles 
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // TFSSpecification::GetFinderInfo: 
  195. //----------------------------------------------------------------------------------------
  196. OSStatus TFSSpecification::GetFinderInfo(FInfo& fndrInfo) const
  197. {
  198.     return FSpGetFInfo(&fFileSpec, &fndrInfo);
  199. } // TFSSpecification::GetFinderInfo 
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // TFSSpecification::SetFinderInfo: 
  203. //----------------------------------------------------------------------------------------
  204. OSStatus TFSSpecification::SetFinderInfo(const FInfo& fndrInfo) const
  205. {
  206.     return FSpSetFInfo(&fFileSpec, &fndrInfo);
  207. } // TFSSpecification::SetFinderInfo 
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // TFSSpecification::CreateFile: 
  211. //----------------------------------------------------------------------------------------
  212. OSStatus TFSSpecification::CreateFile(OSType creator, OSType fileType, ScriptCode scriptTag /* = 0*/)
  213. {
  214.     OSStatus err = FSpCreate(&fFileSpec, creator, fileType, scriptTag);
  215.  
  216.     //
  217.     // 'fileSpec' may contain a relative pathname; call 'FSMakeFSSpec'
  218.     // to make an FSSpec whose name field contains only the name
  219.     // of the file, and none of the path to that file.
  220.     //
  221.     if(err == noErr)
  222.         err = FSMakeFSSpec(fFileSpec.vRefNum, fFileSpec.parID, fFileSpec.name, &fFileSpec);
  223.     
  224.     return err;
  225. } // TFSSpecification::CreateFile 
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // TFSSpecification::CreateNewChildFile: 
  229. //----------------------------------------------------------------------------------------
  230. OSStatus TFSSpecification::CreateNewChildFile(const TString& newName, OSType creator, OSType fileType, TFSSpecification& newFile) const
  231. {
  232.     SInt32 dirID = 0;
  233.     OSStatus err = noErr;
  234.     
  235.     err = this->GetDirID(dirID);
  236.     if(err == noErr)
  237.         err = newFile.MakeFSSpec(this->VRefNum(), dirID, newName);
  238.     if(err == noErr)
  239.         err = newFile.CreateFile(creator, fileType);
  240.  
  241.     return err;
  242. } // TFSSpecification::CreateNewChildFile 
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // TFSSpecification::CreateNewChildFolder: 
  246. //----------------------------------------------------------------------------------------
  247. OSStatus TFSSpecification::CreateNewChildFolder(const TString& newName, TFSSpecification& newFolder) const
  248. {
  249.     TFSSpecification newFolderSpec;
  250.     SInt32 dirID;
  251.     OSStatus err = noErr;
  252.     
  253.     err = this->GetDirID(dirID);
  254.     if(err == noErr)
  255.         err = newFolderSpec.MakeFSSpec(this->VRefNum(), dirID, newName);
  256.     if(err == noErr)
  257.     {
  258.         FSSpec fsSpec = newFolderSpec;
  259.         SInt32 createdDirID;
  260.         err = FSpDirCreate(&fsSpec, 0, &createdDirID);
  261.         newFolder.AdoptSpec(this->VRefNum(), createdDirID);
  262.     }
  263.     
  264.     return err;
  265. } // TFSSpecification::CreateNewChildFolder 
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // TFSSpecification::ContainerSpecification
  269. //----------------------------------------------------------------------------------------
  270. OSStatus TFSSpecification::ContainerSpecification(TFSSpecification& container) const
  271. {
  272.     OSStatus err = noErr;
  273.     
  274.     if(this->ParentDirID() == fsRtParID)
  275.         err = fnfErr;
  276.     else if(this->ParentDirID() == fsRtDirID)
  277.         err = this->VolumeSpecification(container);
  278.     else
  279.         err = container.AdoptSpec(this->VRefNum(), this->ParentDirID());
  280.     
  281.     return err;
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // TFSSpecification::VolumeSpecification
  286. //----------------------------------------------------------------------------------------
  287. OSStatus TFSSpecification::VolumeSpecification(TFSSpecification& volumeObjectSpec) const
  288. {
  289.     FSSpec rootSpec;
  290.     
  291.     //
  292.     // Is this the best thing to do, or should we call volumeObjectSpec->AdoptSpec(this->VRefNum(), fsRtDirID)?
  293.     //
  294.     rootSpec.vRefNum = this->VRefNum();
  295.     rootSpec.parID = fsRtParID;
  296.     rootSpec.name[0] = 0;
  297.     
  298.     volumeObjectSpec = TFSSpecification(rootSpec);
  299.     
  300.     return noErr;
  301. }
  302.  
  303. //----------------------------------------------------------------------------------------
  304. // TFSSpecification::AdoptSpec
  305. //----------------------------------------------------------------------------------------
  306. OSStatus TFSSpecification::AdoptSpec(SInt16 vRefNum, SInt32 dirID)
  307. {
  308.     CInfoPBRec pb;
  309.     
  310.     //
  311.     // Set input fields; ioFDirIndex == -1 means look up
  312.     // by vRefNum and dirID (0 == look up by name, positive
  313.     // numbers == look up n'th child)
  314.     //
  315.     pb.dirInfo.ioFDirIndex = -1;
  316.     pb.dirInfo.ioVRefNum = vRefNum;
  317.     pb.dirInfo.ioDrDirID = dirID;
  318.     
  319.     //
  320.     // Set output fields
  321.     //
  322.     pb.dirInfo.ioNamePtr = fFileSpec.name;
  323.     
  324.     //
  325.     // Set fields needed to make call work correctly
  326.     // (e.g. ioCompletion) and make the GetCatInfo call.
  327.     //
  328.     pb.dirInfo.ioResult = noErr;
  329.     pb.dirInfo.ioCompletion = nil;
  330.     pb.dirInfo.ioFVersNum = 0;
  331.     PBGetCatInfoSync(&pb);
  332.     OSStatus err = pb.dirInfo.ioResult;
  333.     
  334.     if(err == noErr)
  335.     {
  336.         //
  337.         // Copy the results into our result FSSpec
  338.         // (n.b. name was already filled in by
  339.         // GetCatInfo)
  340.         //
  341.         fFileSpec.vRefNum = vRefNum;
  342.         fFileSpec.parID = pb.dirInfo.ioDrDirID;
  343.     }
  344.     
  345.     return err;
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. // TFSSpecification::GetDirID
  350. //----------------------------------------------------------------------------------------
  351. OSStatus TFSSpecification::GetDirID(SInt32& dirID) const
  352. {
  353.     Str255 name;
  354.     CInfoPBRec pb;
  355.     
  356.     //
  357.     // Set input fields; ioFDirIndex == -1 means look up
  358.     // by vRefNum and dirID (0 == look up by name, positive
  359.     // numbers == look up n'th child)
  360.     //
  361.     pb.dirInfo.ioFDirIndex = 0;
  362.     pb.dirInfo.ioVRefNum = this->VRefNum();
  363.     pb.dirInfo.ioDrDirID = this->ParentDirID();
  364.     CopyMemory(fFileSpec.name, name, fFileSpec.name[0] + 1);
  365.     pb.dirInfo.ioNamePtr = name;
  366.     
  367.     //
  368.     // Set fields needed to make call work correctly
  369.     // (e.g. ioCompletion) and make the GetCatInfo call.
  370.     //
  371.     pb.dirInfo.ioResult = noErr;
  372.     pb.dirInfo.ioCompletion = nil;
  373.     pb.dirInfo.ioFVersNum = 0;
  374.     PBGetCatInfoSync(&pb);
  375.     OSStatus err = pb.dirInfo.ioResult;
  376.     
  377.     if(err == noErr)
  378.     {
  379.         dirID = pb.dirInfo.ioDrDirID;
  380.     }
  381.     
  382.     return err;
  383. }
  384.  
  385. //----------------------------------------------------------------------------------------
  386. // TFSSpecification::MakeFSSpec
  387. //----------------------------------------------------------------------------------------
  388. OSStatus TFSSpecification::MakeFSSpec(SInt16 vRefNum, SInt32 dirID, ConstStr255Param name)
  389. {
  390.     FSSpec newSpec;
  391.     
  392.     CopyMemory(name, newSpec.name, name[0] + 1);
  393.     newSpec.vRefNum = vRefNum;
  394.     newSpec.parID = dirID;
  395.     
  396.     return noErr;
  397. }
  398.  
  399. //========================================================================================
  400. // CLASS TOpenFileRefNum
  401. //========================================================================================
  402.  
  403.  
  404. //----------------------------------------------------------------------------------------
  405. // TOpenFileRefNum::Open: 
  406. //----------------------------------------------------------------------------------------
  407. OSStatus TOpenFileRefNum::Open(const TFSSpecification& fileToOpen, SInt8 permission)
  408. {
  409.     OSStatus err = fileToOpen.Open(fRefNum, permission);
  410.     fIsOpen = (err == noErr);
  411.     
  412.     return err;
  413. } // TOpenFileRefNum::Open 
  414.  
  415. //----------------------------------------------------------------------------------------
  416. // TOpenFileRefNum::Close: 
  417. //----------------------------------------------------------------------------------------
  418. OSStatus TOpenFileRefNum::Close()
  419. {
  420.     OSStatus err = FSClose(fRefNum);
  421.     fRefNum = kInvalidOpenRefNum;
  422.     fIsOpen = false;
  423.     
  424.     return err;
  425. } // TOpenFileRefNum::Close 
  426.  
  427. //----------------------------------------------------------------------------------------
  428. // TOpenFileRefNum::Read: 
  429. //----------------------------------------------------------------------------------------
  430. OSStatus TOpenFileRefNum::Read(SInt32 *count, void *buffPtr)
  431. {
  432.     return FSRead(fRefNum, count, buffPtr);
  433. } // TOpenFileRefNum::Read 
  434.  
  435. //----------------------------------------------------------------------------------------
  436. // TOpenFileRefNum::Write: 
  437. //----------------------------------------------------------------------------------------
  438. OSStatus TOpenFileRefNum::Write(SInt32 *count, void *buffPtr)
  439. {
  440.     return FSWrite(fRefNum, count, buffPtr);
  441. } // TOpenFileRefNum::Write 
  442.  
  443. //----------------------------------------------------------------------------------------
  444. // TOpenFileRefNum::GetEndOfFile: 
  445. //----------------------------------------------------------------------------------------
  446. OSStatus TOpenFileRefNum::GetEndOfFile(SInt32* logEOF)
  447. {
  448.     return GetEOF(fRefNum, logEOF);
  449. } // TOpenFileRefNum::GetEndOfFile 
  450.  
  451. //----------------------------------------------------------------------------------------
  452. // TOpenFileRefNum::SetEndOfFile: 
  453. //----------------------------------------------------------------------------------------
  454. OSStatus TOpenFileRefNum::SetEndOfFile(SInt32 logEOF)
  455. {
  456.     return SetEOF(fRefNum, logEOF);
  457. } // TOpenFileRefNum::SetEndOfFile 
  458.  
  459. //----------------------------------------------------------------------------------------
  460. // TOpenFileRefNum::GetFilePosition: 
  461. //----------------------------------------------------------------------------------------
  462. OSStatus TOpenFileRefNum::GetFilePosition(SInt32* filePos)
  463. {
  464.     return GetFPos(fRefNum, filePos);
  465. } // TOpenFileRefNum::GetFilePosition 
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // TOpenFileRefNum::SetFilePosition: 
  469. //----------------------------------------------------------------------------------------
  470. OSStatus TOpenFileRefNum::SetFilePosition(SInt16 posMode, SInt32 posOff)
  471. {
  472.     return SetFPos(fRefNum, posMode, posOff);
  473. } // TOpenFileRefNum::SetFilePosition 
  474.  
  475. //----------------------------------------------------------------------------------------
  476. // TOpenFileRefNum::SetFilePosition: 
  477. //----------------------------------------------------------------------------------------
  478. OSStatus TOpenFileRefNum::SetFilePosition(SInt32 posOff)
  479. {
  480.     return this->SetFilePosition(fsFromStart, posOff);
  481. } // TOpenFileRefNum::SetFilePosition 
  482.  
  483.  
  484.  
  485.